package com.fjb.controller.recycle;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.DataSource;
import com.fjb.common.enums.recycle.RecycleStatus;
import com.fjb.common.utils.SerialNumberUtils;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.recycle.RecycleInfo;
import com.fjb.service.recycle.RecycleInfoService;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 收购信息表 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2020-02-15
 */
@Controller
@RequestMapping("/recycle")
public class RecycleInfoController extends BaseController{
	
	@Autowired
	private RecycleInfoService recycleInfoService;
	
	/**
	 * @Description:添加回收信息
	 * @param request
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月15日 下午7:29:55
	 */
	@RequestMapping(value="/addInfo")
	@ResponseBody
	public JsonResult<Integer> addInfo(HttpServletRequest request,BigDecimal recyclePrice,
			Integer recycleCount,Integer brandId ,Integer categoryId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null) {
			httpCode.setMsg("品牌不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(categoryId==null) {
			httpCode.setMsg("车型不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(recyclePrice==null) {
			httpCode.setMsg("收购价格不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(recycleCount==null) {
			httpCode.setMsg("收购数量不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String recycleName = request.getParameter("recycleName");
		if(StringUtils.isBlank(recycleName)) {
			httpCode.setMsg("收购名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}	
		String recyclePhone = request.getParameter("recyclePhone");
		if(StringUtils.isBlank(recyclePhone)) {
			httpCode.setMsg("收购电话不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String recycleFiles = request.getParameter("recycleFiles");
		if(StringUtils.isBlank(recycleFiles)) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String recycleDetails = request.getParameter("recycleDetails");
		if(StringUtils.isBlank(recycleDetails)) {
			httpCode.setMsg("收购内容不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String[] filesSplit = recycleFiles.split(",");
		String recycleMianFiles = "";
		for (String fileStr : filesSplit) {
			recycleMianFiles = fileStr;
			break;
		}
		
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		
		String recycleNumber = SerialNumberUtils.createNumber(SerialNumberUtils.RECYCLE_INFO, mainUserId);
		
		RecycleInfo ri = new RecycleInfo();
		ri.setBrandId(brandId);	
		ri.setCategoryId(categoryId);
		ri.setRecycleNumber(recycleNumber);
		ri.setRecycleName(recycleName);
		ri.setRecyclePhone(recyclePhone);
		ri.setRecyclePrice(recyclePrice);	
		ri.setRecycleMianFiles(recycleMianFiles);
		ri.setRecycleFiles(recycleFiles);
		ri.setRecycleCount(recycleCount);
		ri.setMainUserId(mainUserId);
		ri.setUserId(userId);
		ri.setCreateUserId(userId);
		ri.setCreateTime(LocalDateTime.now());
		ri.setRecycleStatus(RecycleStatus.STATUS_1.getCode());
		ri.setDataSource(DataSource.WX_APPLET.getCode());
		ri.setViewCount(0);
		boolean b = recycleInfoService.save(ri);
		if(b==false) {	
			httpCode.setMsg("保存失败,请联系管理员");
			return new JsonResult<Integer>(null, httpCode);
		}	
		return new JsonResult<Integer>(null, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改收购信息
	 * @param request
	 * @param recycleId
	 * @param recyclePrice
	 * @param recycleCount
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月15日 下午7:51:20
	 */
	@RequestMapping(value="/updaterInfo")
	@ResponseBody
	public JsonResult<Integer> updaterInfo(HttpServletRequest request,Integer recycleId,BigDecimal recyclePrice,
			Integer recycleCount){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(recycleId==null) {
			return new JsonResult<Integer>(null, httpCode);
		}
		
		return new JsonResult<Integer>(null, HttpCode.SUCCESS);
	}
}
